//
//  SRHistoryData.m
//  sr01sdkProject
//
//  Created by Linktop on 2022/5/21.
//

#import "SRHistoryData.h"

@implementation SRHistoryData

- (instancetype)initWithData:(NSData *)data
{
    if (self = [super init]) {
        
        if (data.length < 20) {
            return self;
        }
        uint8_t *dataBytes = (uint8_t *)[data bytes];
        // 四字节时间戳
        //2345
//        uint32_t timeStamp = (dataBytes[2] << 24) +  (dataBytes[3] << 16) + (dataBytes[4] << 8) + dataBytes[5];
        
        uint32_t timeStamp = *((uint32_t *)(dataBytes + 2));//(dataBytes[2] << 24) +  (dataBytes[3] << 16) + (dataBytes[4] << 8) + dataBytes[5];
//        DebugNSLog(@"timeStamp = 0X%x timeStamp2 = 0X%x", timeStamp, timeStamp2);

        _timeStamp = @(timeStamp);
        
        UInt8 status = dataBytes[6];  // 状态
        [self analyStatus:status];
        
        
        uint8_t heartRate =  dataBytes[7];  // 心率
        if (heartRate <= 200 && heartRate > 0) {
            _heartRate = @(heartRate);
        }
       
        
        uint16_t hrv = (dataBytes[13] << 8) + dataBytes[14]; // hrv
        _hrv = @(sqrtf(hrv));
        
        uint8_t oxy = dataBytes[15]; // 血氧
        _bloodOxygen = @(oxy);
        
        float skinTemp = (dataBytes[16] + 200) / 10.0f; // 体表温度
        _skinTemp = @(skinTemp);
        
        uint16_t steps = (dataBytes[17] << 8) + dataBytes[18]; // 步数
        if (steps > 0 && steps < 65535) {
            _steps = @(steps);
        }
        
        
        
    }
    return self;
}


-(void)analyStatus:(uint8_t)statusByte {
    
    BOOL isSos = ((statusByte >> 6) & 0X01) == 1;
    _isSos = isSos;
    
    NSUInteger soprtTimes = statusByte & 0X3F;  // 运动触发次数
    _soprtTimes = @(soprtTimes);
}

@end
